{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 尝试半监督模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from frameworks.CPLELearning import CPLELearningModel\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "from sklearn import metrics\n",
    "from sklearn import preprocessing\n",
    "from sklearn.semi_supervised import label_propagation\n",
    "\n",
    "os.chdir(\"C:/Users/Ma/Desktop/document/企业经营退出风险预测/analysis\")\n",
    "data_train = pd.read_csv('data_train.csv',encoding='gb2312')\n",
    "data_test = pd.read_csv('data_test.csv',encoding='gb2312')\n",
    "\n",
    "from xgboost.sklearn import XGBClassifier\n",
    "# from sklearn.metrics import f1_score\n",
    "from sklearn.cross_validation import train_test_split\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "\n",
    "targets = data_train['TARGET']\n",
    "data_train = data_train.drop(labels=['TARGET'],axis=1)\n",
    "data_train = np.array(data_train)\n",
    "EID = data_test['EID']\n",
    "data_test = np.array(data_test)\n",
    "targets = np.array(targets)\n",
    "\n",
    "#  划分样本集\n",
    "# train_x,test_x,train_y,test_y = train_test_split(train_data,targets,test_size=0.5,random_state=66)\n",
    "\n",
    "# 设置参数\n",
    "xgb = XGBClassifier(n_estimators = 1350,scale_pos_weight=4,nthread=-1,seed=6,max_depth=3,min_child_weight=6,learning_rate=0.05,\n",
    "\t\t\t\t\t\t\tgamma=0,subsample=0.9,colsample_bytree=0.9,reg_alpha=8)\n",
    "# # 训练\n",
    "# xgb.fit(train_x, train_y)\n",
    "# # 预测\n",
    "# pre_y = xgb.predict_proba(test_x)[:,1]\n",
    "# pre_y_categ = xgb.predict(test_x)\n",
    "# # 计算auc\n",
    "# fpr, tpr, thresholds = metrics.roc_curve(test_y, pre_y)\n",
    "# auc=metrics.auc(fpr, tpr)\n",
    "# f1 = metrics.f1_score(test_y,pre_y_categ)\n",
    "# print(\"AUC得分为：\")\n",
    "# print(auc)\n",
    "# print('f1-score为：')\n",
    "# print(f1)\n",
    "\n",
    "# 半监督\n",
    "train = np.concatenate([data_train,data_test],axis=0)\n",
    "ys = np.concatenate([targets,[-1]*data_test.shape[0]])\n",
    "\n",
    "from frameworks.SelfLearning import SelfLearningModel\n",
    "from frameworks.CPLELearning import CPLELearningModel\n",
    "# ssmodel = SelfLearningModel(xgb,max_iter=2)\n",
    "ssmodel = CPLELearningModel(xgb)\n",
    "# Train the model and use it to predict\n",
    "ssmodel.fit(train, ys)\n",
    "pre_y = ssmodel.predict_proba(test_x)[:,1]\n",
    "pre_y_categ = xgb.predict(test_data)\n",
    "\n",
    "evaluation_public = pd.DataFrame()\n",
    "evaluation_public['EID'] = EID\n",
    "evaluation_public['FORTARGET']=pre_y_categ\n",
    "evaluation_public['PROB'] = pre_y\n",
    "evaluation_public.to_csv('evaluation_public.csv',index=False)\n",
    "# fpr, tpr, thresholds = metrics.roc_curve(test_y, pre_y)\n",
    "# auc=metrics.auc(fpr, tpr)\n",
    "# print(\"semi_AUC得分为：\")\n",
    "# print(auc)\n",
    "# 画出特征重要性图\n",
    "# features = list(train_data.columns)\n",
    "# feature_important = xgb.feature_importances_\n",
    "# plt.figure(figsize=(16,4))\n",
    "# plt.bar(np.arange(len(features)),feature_important)\n",
    "# plt.xticks(np.arange(len(features)),features,fontsize=10,rotation=15)\n",
    "# plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "?CPLELearningModel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "semi_AUC得分为：\n",
      "0.661904251618\n"
     ]
    }
   ],
   "source": [
    "fpr, tpr, thresholds = metrics.roc_curve(test_y, pre_y)\n",
    "auc=metrics.auc(fpr, tpr)\n",
    "print(\"semi_AUC得分为：\")\n",
    "print(auc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(153006, 14)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2142084,)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
